using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed;
    private Rigidbody2D rgbody;
    private Animator animator;
    private float inputX, inputY;
    private float stopX, stopY;
    //private Vector3 offset;
    void Awake()
    {
        rgbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        //offset = Camera.main.transform.position - transform.position;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // if(Input.GetKeyDown(KeyCode.W))
        // Debug.Log(Input.GetAxisRaw("Vertical"));
        inputX = Input.GetAxisRaw("Horizontal");
        inputY = Input.GetAxisRaw("Vertical");//返回-1，0，1三个之一的常数
        Vector2 input = (transform.right * inputX + transform.up * inputY).normalized;
        rgbody.velocity = input * speed;

        if (input != Vector2.zero)
        {
            animator.SetBool("isMoving", true);
            stopX = inputX;
            stopY = inputY;
        }
        else
            animator.SetBool("isMoving", false);
        animator.SetFloat("InputX", stopX);
        animator.SetFloat("InputY", stopY);

        //Camera.main.transform.position = transform.position + offset;
    }
}
